/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-12-05
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.business.job.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.customer.entity.BasicCustomer;
import org.jeecg.modules.basic.customer.service.IBasicCustomerService;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.basic.materialtype.entity.BasicMaterialType;
import org.jeecg.modules.basic.materialtype.service.IBasicMaterialTypeService;
import org.jeecg.modules.basic.supplier.entity.BasicSupplier;
import org.jeecg.modules.basic.supplier.service.IBasicSupplierService;
import org.jeecg.modules.basic.unit.entity.BasicUnit;
import org.jeecg.modules.basic.unit.service.IBasicUnitService;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroup;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupService;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTaskMaterial;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskMaterialService;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptMaterialService;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.job.service.CheckService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlan;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanDifferenceService;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用检查轮询服务实现类
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-12-05
 * @description 类描述
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CheckServiceImpl implements CheckService {
    private String STR_UNUSED = "0";

    private String STR_USED = "1";

    private Integer INT_UNUSED = 0;

    private Integer INT_USED = 1;

    private final IWmsBasicWarehouseService iWmsBasicWarehouseService;

    private final IWmsBasicWarehouseGroupService iWmsBasicWarehouseGroupService;

    private final IBasicCustomerService iBasicCustomerService;

    private final IBasicSupplierService iBasicSupplierService;

    private final IBasicMaterialTypeService iBasicMaterialTypeService;

    private final IBasicMaterialService iBasicMaterialService;

    private final IBasicUnitService iBasicUnitService;

    private final IWmsInReceiptTaskService iWmsInReceiptTaskService;

    private final IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;

    private final IWmsOutReceiptTaskService iWmsOutReceiptTaskService;

    private final IWmsOutReceiptTaskMaterialService iWmsOutReceiptTaskMaterialService;

    private final IWmsInReceiptService iWmsInReceiptService;

    private final IWmsInReceiptMaterialService iWmsInReceiptMaterialService;

    private final IWmsOutReceiptService iWmsOutReceiptService;

    private final IWmsOutReceiptMaterialService iWmsOutReceiptMaterialService;

    private final IWmsMoveReceiptService iWmsMoveReceiptService;

    private final IWmsMoveReceiptMaterialService iWmsMoveReceiptMaterialService;

    private final IWmsDeliveryTaskService iWmsDeliveryTaskService;

    private final IWmsDeliveryTaskMaterialService iWmsDeliveryTaskMaterialService;

    private final ITakeStockPlanService iTakeStockPlanService;




    /**
     * 更新已使用字段信息实现
     *
     * @author DongPeng
     * @date 2023/12/5 9:17
     * @param null
     * @return
     */
    @Override
    public void checkUsed() {
        //仓库--1
        log.info("开始更新仓库使用情况");
        List<WmsBasicWarehouse> warehouseList = iWmsBasicWarehouseService.list();
        warehouseList.forEach(warehouse -> {
            if (ObjectUtil.isEmpty(warehouse.getIsUse())) {
                warehouse.setIsUse(STR_UNUSED);
            }else {
                //查询是否有使用记录
                warehouse.setIsUse(query(1, warehouse.getId()) ? STR_USED : STR_UNUSED);
            }
        });
        iWmsBasicWarehouseService.updateBatchById(warehouseList);

        //仓库组--2
        log.info("开始更新仓库组使用情况");
        List<WmsBasicWarehouseGroup> warehouseGroups = iWmsBasicWarehouseGroupService.list();
        warehouseGroups.forEach(group -> {
            if (ObjectUtil.isEmpty(group.getUsed())) {
                group.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                group.setUsed(query(2, group.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iWmsBasicWarehouseGroupService.updateBatchById(warehouseGroups);

        //客户--3
        log.info("开始更新客户使用情况");
        List<BasicCustomer> customers = iBasicCustomerService.list();
        customers.forEach(customer -> {
            if (ObjectUtil.isEmpty(customer.getUsed())) {
                customer.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                customer.setUsed(query(3, customer.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iBasicCustomerService.updateBatchById(customers);

        //供应商--4
        log.info("开始更新供应商使用情况");
        List<BasicSupplier> suppliers = iBasicSupplierService.list();
        suppliers.forEach(supplier -> {
            if (ObjectUtil.isEmpty(supplier.getUsed())) {
                supplier.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                supplier.setUsed(query(4, supplier.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iBasicSupplierService.updateBatchById(suppliers);

        //物料分类--5
        log.info("开始更新物料分类使用情况");
        List<BasicMaterialType> types = iBasicMaterialTypeService.list();
        types.forEach( type -> {
            if (ObjectUtil.isEmpty(type.getUsed())) {
                type.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                type.setUsed(query(5, type.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iBasicMaterialTypeService.updateBatchById(types);

        //物料信息--6
        log.info("开始更新物料信息使用情况");
        List<BasicMaterial> materials = iBasicMaterialService.list();
        materials.forEach(material -> {
            if (ObjectUtil.isEmpty(material.getUsed())) {
                material.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                material.setUsed(query(6, material.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iBasicMaterialService.updateBatchById(materials);

        //计量单位--7
        log.info("开始更新计量单位使用情况");
        List<BasicUnit> units = iBasicUnitService.list();
        units.forEach(unit -> {
            if (ObjectUtil.isEmpty(unit.getUsed())) {
                unit.setUsed(INT_UNUSED);
            }else {
                //查询是否有使用记录
                unit.setUsed(query(7, unit.getId()) ? INT_USED : INT_UNUSED);
            }
        });
        iBasicUnitService.updateBatchById(units);
        log.info("所有基础信息更新完毕 ------------> 任务完成");
    }


    private boolean query(Integer type, String id) {
        boolean result = false;
        switch (type) {
            case 1 : {
                //入库任务
                long inReceiptMaterials = iWmsInReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId, id));
                if (inReceiptMaterials > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outReceiptTaskMaterials = iWmsOutReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getWmsBasicMaterialId, id));
                if (outReceiptTaskMaterials > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inMaterials = iWmsInReceiptMaterialService.count(new LambdaQueryWrapper<WmsInReceiptMaterial>().eq(WmsInReceiptMaterial::getWmsBasicWarehouseId, id));
                if (inMaterials > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outMaterials = iWmsOutReceiptMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptMaterial>().eq(WmsOutReceiptMaterial::getWmsBasicWarehouseId, id));
                if (outMaterials > 0) {
                    result = true;
                    break;
                }
                //移库单
                long moveCount = iWmsMoveReceiptService.count(new LambdaQueryWrapper<WmsMoveReceipt>().eq(WmsMoveReceipt::getTransferFromWarehouseId, id));
                if (moveCount > 0) {
                    result = true;
                    break;
                }
                //调拨任务
                long deliveryCount = iWmsDeliveryTaskService.count(new LambdaQueryWrapper<WmsDeliveryTask>().eq(WmsDeliveryTask::getInWarehouseId, id).or().eq(WmsDeliveryTask::getOutWarehouseId, id));
                if (deliveryCount > 0) {
                    result = true;
                    break;
                }
                //盘点任务
                long takeCount = iTakeStockPlanService.count(new LambdaQueryWrapper<TakeStockPlan>().eq(TakeStockPlan::getWmsBasicWarehouse, id));
                if (takeCount > 0) {
                    result = true;
                    break;
                }
                break;
            }

            case 2 : {
                //入库任务
                long inTask = iWmsInReceiptTaskService.count(new LambdaQueryWrapper<WmsInReceiptTask>().eq(WmsInReceiptTask::getWmsBasicWarehouseGroupId, id));
                if (inTask > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outTask = iWmsOutReceiptTaskService.count(new LambdaQueryWrapper<WmsOutReceiptTask>().eq(WmsOutReceiptTask::getWmsBasicWarehouseGroupId, id));
                if (outTask > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inReceipt = iWmsInReceiptService.count(new LambdaQueryWrapper<WmsInReceipt>().eq(WmsInReceipt::getWmsBasicWarehouseGroupId, id));
                if (inReceipt > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outReceipt = iWmsOutReceiptService.count(new LambdaQueryWrapper<WmsOutReceipt>().eq(WmsOutReceipt::getWmsBasicWarehouseGroupId, id));
                if (outReceipt > 0) {
                    result = true;
                    break;
                }
                //移库单
                long moveCount = iWmsMoveReceiptService.count(new LambdaQueryWrapper<WmsMoveReceipt>().eq(WmsMoveReceipt::getWmsBasicWarehouseGroupId, id));
                if (moveCount > 0) {
                    result = true;
                    break;
                }
                //调拨任务
                long deliveryCount = iWmsDeliveryTaskService.count(new LambdaQueryWrapper<WmsDeliveryTask>().eq(WmsDeliveryTask::getWmsBasicWarehouseGroupId, id));
                if (deliveryCount > 0) {
                    result = true;
                    break;
                }
                //盘点任务
                long takeCount = iTakeStockPlanService.count(new LambdaQueryWrapper<TakeStockPlan>().eq(TakeStockPlan::getWmsBasicWarehouseGroupId, id));
                if (takeCount > 0) {
                    result = true;
                    break;
                }
                break;
            }
            case 3 : {
                //入库任务
                long inTask = iWmsInReceiptTaskService.count(new LambdaQueryWrapper<WmsInReceiptTask>().eq(WmsInReceiptTask::getWmsBasicSupplierId, id));
                if (inTask > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outTask = iWmsOutReceiptTaskService.count(new LambdaQueryWrapper<WmsOutReceiptTask>().eq(WmsOutReceiptTask::getCustomer, id));
                if (outTask > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inReceipt = iWmsInReceiptService.count(new LambdaQueryWrapper<WmsInReceipt>().eq(WmsInReceipt::getWmsBasicSupplierId, id));
                if (inReceipt > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outReceipt = iWmsOutReceiptService.count(new LambdaQueryWrapper<WmsOutReceipt>().eq(WmsOutReceipt::getCustomer, id));
                if (outReceipt > 0) {
                    result = true;
                    break;
                }
                //盘点任务
                long takeCount = iTakeStockPlanService.count(new LambdaQueryWrapper<TakeStockPlan>().eq(TakeStockPlan::getCustomerId, id));
                if (takeCount > 0) {
                    result = true;
                    break;
                }
                break;
            }
            case 4 : {
                //入库任务
                long inTask = iWmsInReceiptTaskService.count(new LambdaQueryWrapper<WmsInReceiptTask>().eq(WmsInReceiptTask::getSupplier, id));
                if (inTask > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outTask = iWmsOutReceiptTaskService.count(new LambdaQueryWrapper<WmsOutReceiptTask>().eq(WmsOutReceiptTask::getSupplier, id));
                if (outTask > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inReceipt = iWmsInReceiptService.count(new LambdaQueryWrapper<WmsInReceipt>().eq(WmsInReceipt::getSupplier, id));
                if (inReceipt > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outReceipt = iWmsOutReceiptService.count(new LambdaQueryWrapper<WmsOutReceipt>().eq(WmsOutReceipt::getSupplier, id));
                if (outReceipt > 0) {
                    result = true;
                    break;
                }
                //盘点任务
                long takeCount = iTakeStockPlanService.count(new LambdaQueryWrapper<TakeStockPlan>().eq(TakeStockPlan::getCustomerId, id));
                if (takeCount > 0) {
                    result = true;
                    break;
                }
                break;
            }
            case 5 : {
                //物料分类
                long typeCount = iBasicMaterialTypeService.count(new LambdaQueryWrapper<BasicMaterialType>().eq(BasicMaterialType::getPid, id));
                if (typeCount > 0) {
                    result = true;
                    break;
                }
                //物料信息
                long infoCount = iBasicMaterialService.count(new LambdaQueryWrapper<BasicMaterial>().eq(BasicMaterial::getMaterialTypeId, id));
                if (infoCount > 0) {
                    result = true;
                    break;
                }
                break;
            }
            case 6 : {
                //入库任务
                long inReceiptMaterials = iWmsInReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getWmsBasicMaterialId, id));
                if (inReceiptMaterials > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outReceiptTaskMaterials = iWmsOutReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getWmsBasicMaterialId, id));
                if (outReceiptTaskMaterials > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inMaterials = iWmsInReceiptMaterialService.count(new LambdaQueryWrapper<WmsInReceiptMaterial>().eq(WmsInReceiptMaterial::getWmsBasicMaterialId, id));
                if (inMaterials > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outMaterials = iWmsOutReceiptMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptMaterial>().eq(WmsOutReceiptMaterial::getWmsBasicMaterialId, id));
                if (outMaterials > 0) {
                    result = true;
                    break;
                }
                //移库单
                long moveMaterials = iWmsMoveReceiptMaterialService.count(new LambdaQueryWrapper<WmsMoveReceiptMaterial>().eq(WmsMoveReceiptMaterial::getWmsBasicMaterialId, id));
                if (moveMaterials > 0) {
                    result = true;
                    break;
                }
                //调拨任务
                long deliveryMaterials = iWmsDeliveryTaskMaterialService.count(new LambdaQueryWrapper<WmsDeliveryTaskMaterial>().eq(WmsDeliveryTaskMaterial::getWmsBasicMaterialId, id));
                if (deliveryMaterials > 0) {
                    result = true;
                    break;
                }
                //盘点任务
                long takeCount = iTakeStockPlanService.count(new LambdaQueryWrapper<TakeStockPlan>().eq(TakeStockPlan::getMaterialId, id));
                if (takeCount > 0) {
                    result = true;
                    break;
                }
                break;
            }
            case 7 : {
                //入库任务
                long inReceiptMaterials = iWmsInReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getWmsBasicUnitId, id).or().eq(WmsInReceiptTaskMaterial::getUnitAssistId, id));
                if (inReceiptMaterials > 0) {
                    result = true;
                    break;
                }
                //出库任务
                long outReceiptTaskMaterials = iWmsOutReceiptTaskMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getWmsBasicUnitId, id).or().eq(WmsOutReceiptTaskMaterial::getUnitAssistId, id));
                if (outReceiptTaskMaterials > 0) {
                    result = true;
                    break;
                }
                //入库单
                long inMaterials = iWmsInReceiptMaterialService.count(new LambdaQueryWrapper<WmsInReceiptMaterial>().eq(WmsInReceiptMaterial::getWmsBasicUnitId, id).or().eq(WmsInReceiptMaterial::getUnitAssistId, id));
                if (inMaterials > 0) {
                    result = true;
                    break;
                }
                //出库单
                long outMaterials = iWmsOutReceiptMaterialService.count(new LambdaQueryWrapper<WmsOutReceiptMaterial>().eq(WmsOutReceiptMaterial::getWmsBasicUnitId, id).or().eq(WmsOutReceiptMaterial::getUnitAssistId, id));
                if (outMaterials > 0) {
                    result = true;
                    break;
                }
                //移库单
                long moveMaterials = iWmsMoveReceiptMaterialService.count(new LambdaQueryWrapper<WmsMoveReceiptMaterial>().eq(WmsMoveReceiptMaterial::getWmsBasicUnitId, id).or().eq(WmsMoveReceiptMaterial::getUnitAssistId, id));
                if (moveMaterials > 0) {
                    result = true;
                    break;
                }
                //调拨任务
                long deliveryMaterials = iWmsDeliveryTaskMaterialService.count(new LambdaQueryWrapper<WmsDeliveryTaskMaterial>().eq(WmsDeliveryTaskMaterial::getWmsBasicUnitId, id).or().eq(WmsDeliveryTaskMaterial::getUnitAssistId, id));
                if (deliveryMaterials > 0) {
                    result = true;
                    break;
                }
                break;
            }
            default: break;
        }
        return result;
    }
}
